import 'dart:math';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:mall/gen/assets.gen.dart';

class ImageComponent extends StatelessWidget {
  const ImageComponent(
    this.imagePath, {
    super.key,
    this.size,
    this.height,
    this.width,
    this.color,
    this.backgroundColor,
    this.progressColor = Colors.blue,
    this.margin,
    this.padding,
    this.alignment,
    this.boxFit = BoxFit.fill,
    this.borderRadius = BorderRadius.zero,
    this.border,
    this.isCircle = false,
    this.errorImage,
    this.placeholderImage,
  });

  /// 图片地址
  final String? imagePath;

  /// 尺寸（宽高一致时可以使用此属性）
  final double? size;

  /// 高度
  final double? height;

  /// 宽度
  final double? width;

  /// 颜色
  final Color? color;

  /// 背景色
  final Color? backgroundColor;

  /// 进度条颜色
  final Color progressColor;

  /// 外边距
  final EdgeInsetsGeometry? margin;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 对齐
  final AlignmentGeometry? alignment;

  /// 显示模式
  final BoxFit? boxFit;

  /// 圆角
  final BorderRadiusGeometry borderRadius;

  /// 边框
  final Border? border;

  /// 是否是圆形
  final bool isCircle;

  /// 错误图片
  final String? errorImage;

  /// 占位图片
  final String? placeholderImage;

  double get _imageW => size ?? width ?? 100.0;
  double get _imageH => size ?? height ?? 100.0;
  double get _realImageW => isCircle ? min(_imageW, _imageH) : _imageW;
  double get _realImageH => isCircle ? min(_imageW, _imageH) : _imageH;

  Widget _assetsImage(String path) {
    return Image.asset(
      path,
      color: color,
      fit: boxFit,
      width: double.infinity,
      height: double.infinity,
      colorBlendMode: BlendMode.color,
    );
  }

  Widget _netImage(String url) {
    return CachedNetworkImage(
      imageUrl: url,
      fit: boxFit,
      color: color,
      width: double.infinity,
      height: double.infinity,
      colorBlendMode: BlendMode.color,
      placeholder: (context, url) {
        return _placeholderImage();
      },
      // errorWidget: (context, url, error) {
      //   return _assetsImage(errorImage ?? R.image.imgError);
      // },
    );
  }

  Widget _svgImage(String url) {
    return SvgPicture.asset(
      url,
      width: double.infinity,
      height: double.infinity,
      colorFilter: color != null ? ColorFilter.mode(color!, BlendMode.srcIn) : null,
    );
  }

  Widget _placeholderImage() {
    return placeholderImage == null
        ? Center(
            child: CircularProgressIndicator(
              color: progressColor,
              strokeWidth: 2,
            ),
          )
        : _assetsImage(placeholderImage!);
  }

  @override
  Widget build(BuildContext context) {
    String? imageUrl = imagePath;

    if (imageUrl == null) {
      imageUrl = placeholderImage ?? Assets.imgs.placeholderImage.path;
    }
    if (imageUrl.isEmpty) {
      imageUrl = placeholderImage ?? Assets.imgs.placeholderImage.path;
    }
    bool isNet = imageUrl.startsWith('http://') || imageUrl.startsWith('https://');
    bool isSvg = imageUrl.endsWith('.svg');

    return Container(
      width: _realImageW,
      height: _realImageH,
      margin: margin,
      padding: padding,
      alignment: alignment,
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
        borderRadius: isCircle ? BorderRadius.all(Radius.circular(_realImageH * 0.5)) : borderRadius,
        border: border,
        color: backgroundColor,
      ),
      child: ClipRRect(
        borderRadius: isCircle ? BorderRadius.all(Radius.circular(_realImageH * 0.5)) : borderRadius,
        child: isNet ? _netImage(imageUrl) : (isSvg ? _svgImage(imageUrl) : _assetsImage(imageUrl)),
      ),
    );
  }
}
